package com.jt.controller;

import com.jt.entity.Order;
import com.jt.entity.Result;
import com.jt.feighService.StorageFeignService;
import com.jt.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.UUID;

@RestController
@RequestMapping("/order")
@Api(value = "订单业务接口", tags = {"订单接口"})
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private StorageFeignService storageFeignService;

    @RequestMapping("/insertOrder")
    // 在此方法上开启全局事务
    @GlobalTransactional(name = "createOrder", rollbackFor = Exception.class)
    @ApiOperation("扣减库存")
    public Result getOrder() {
        Order order = new Order();
        order.setId(UUID.randomUUID().toString().substring(6));
        order.setUser_id("111111");
        order.setProduct_id("222222");
        order.setCount(10);
        order.setMoney(new BigDecimal("10000"));
        boolean save = orderService.save(order);
        if (save) {
            /*扣减库存*/
            return storageFeignService.deleteStorage(order);

        }
        return null;
    }
}
